package com.iqiyi.pps.epg.core.model.carousel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by karkarrotxu on 2015/7/10.
 */
public class ProgramNode {

    public static final int NODE_NORMAL = 0;
    public static final int NODE_SKIP = 1;
    public static final int NODE_OVER_DAY=3;
    private int nodeType = 0;
    private long usedTime = 0l;
    private long startTime = 0l;
    private long endTime = 0l;
    private List<ProgramNodeElement> children = new ArrayList<ProgramNodeElement>();

    public ProgramNode() {
        this(NODE_NORMAL);
    }

    public ProgramNode(int nodeType) {
        this.nodeType = nodeType;
    }

    public long getUsedTime() {
        return usedTime;
    }

    public void setUsedTime(long usedTime) {
        this.usedTime = usedTime;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        if (this.endTime < endTime) {
            this.endTime = endTime;
        }
    }

    public List<ProgramNodeElement> getChildren() {
        return children;
    }

    public void setChildren(List<ProgramNodeElement> children) {
        this.children = children;
    }

    public int getNodeType() {
        return nodeType;
    }

    public void setNodeType(int nodeType) {
        this.nodeType = nodeType;
    }

    public boolean addChild(ProgramNodeElement child) {
        if (startTime + usedTime + child.getDuration() + child.getAdvertDuration()  <= endTime) {
            children.add(child);
            usedTime += child.getDuration()+ child.getAdvertDuration();
            return true;
        }
        return false;
    }

    public void addEndChild( ProgramNodeElement child){
        children.add(child);
    }

    public void addSkipChild(ProgramNodeElement child) {
        if (endTime < child.getEndPlayTime()) {
            endTime = child.getEndPlayTime();
        }
        children.add(child);
    }

    public void addLiveChild(ProgramNodeElement child) {
        children.add(child);
        if(startTime == 0){
            startTime = child.getAdvertStartTime();
        }
        if (endTime < child.getEndPlayTime()) {
            endTime  = child.getEndPlayTime();
        }
        usedTime = endTime - startTime;
    }

    public boolean hasChildren(){
        return children.size() > 0;
    }
}
